package dingtalk

import (
	"errors"
	"fmt"
	"unsafe"

	"gitee.com/lorock/go-dingtalk-sdk/dingtalkhttplib"

	jsoniter "github.com/json-iterator/go"
	log "github.com/sirupsen/logrus"
)

// 钉钉消息通知
// 工作通知消息：是以企业工作通知会话中某个微应用的名义推送到员工的通知消息，例如生日祝福、入职提醒等。
// 群消息：是指可以调用接口以系统名义向群里推送群聊消息。
// 普通消息：是指员工个人在使用应用时，可以通过界面操作的方式往群或其他人的会话里推送消息，例如发送日志的场景。
// https://ding-doc.dingtalk.com/doc#/serverapi2/al5qyp

// WorkNotificationV2Add 发送工作通知消息
func (c *Client) WorkNotificationV2Add(info WorkNotificationV2) (*WorkNotificationV2Rsp, error) {
	rsp := new(WorkNotificationV2Rsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	// 获取 accessToken
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}
	param := map[string]string{
		"access_token": accessToken,
	}
	url := fmt.Sprintf("%s?%s", MessageCorpconversationAsyncsendV2, dingtalkhttplib.URLEncode(param))
	log.Println(url)
	req := dingtalkhttplib.Post(url)
	// 添加请求body数据
	jsonData, err := json.Marshal(info)
	if err != nil {
		return rsp, err
	}
	str2 := string(jsonData[:])
	log.Println(str2)
	req.Body(jsonData)

	b, err := req.AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&b))
	log.Println(*str)
	err = json.Unmarshal(b, rsp)
	if err != nil {
		return rsp, err
	}
	if rsp.Errcode != 0 {
		return rsp, errors.New(rsp.Errmsg)
	}
	return rsp, nil
}

// WorkNotificationV2Remove 工作通知消息撤回
func (c *Client) WorkNotificationV2Remove(info WorkNotificationCancel) (*WorkNotificationCancelRsp, error) {
	rsp := new(WorkNotificationCancelRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	// 获取 accessToken
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}
	param := map[string]string{
		"access_token": accessToken,
	}
	url := fmt.Sprintf("%s?%s", MessageCorpconversationRecall, dingtalkhttplib.URLEncode(param))
	log.Println(url)
	req := dingtalkhttplib.Post(url)
	// 添加请求body数据
	jsonData, err := json.Marshal(info)
	if err != nil {
		return rsp, err
	}
	str2 := string(jsonData[:])
	log.Println(str2)
	req.Body(jsonData)

	b, err := req.AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&b))
	log.Println(*str)
	err = json.Unmarshal(b, rsp)
	if err != nil {
		return rsp, err
	}
	if rsp.Errcode != 0 {
		return rsp, errors.New(rsp.Errmsg)
	}
	return rsp, nil
}

// WorkNotificationV2GetSendProgress 查询工作通知消息的发送进度
func (c *Client) WorkNotificationV2GetSendProgress(info WorkNotificationGetSendProgress) (*WorkNotificationGetSendProgressRsp, error) {
	rsp := new(WorkNotificationGetSendProgressRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	// 获取 accessToken
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}
	param := map[string]string{
		"access_token": accessToken,
	}
	url := fmt.Sprintf("%s?%s", MessageCorpconversationGetsendprogress, dingtalkhttplib.URLEncode(param))
	log.Println(url)
	req := dingtalkhttplib.Post(url)
	// 添加请求body数据
	jsonData, err := json.Marshal(info)
	if err != nil {
		return rsp, err
	}
	str2 := string(jsonData[:])
	log.Println(str2)
	req.Body(jsonData)

	b, err := req.AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&b))
	log.Println(*str)
	err = json.Unmarshal(b, rsp)
	if err != nil {
		return rsp, err
	}
	if rsp.Errcode != 0 {
		return rsp, errors.New(rsp.Errmsg)
	}
	return rsp, nil
}

// WorkNotificationV2GetSendResult 查询工作通知消息的发送结果
func (c *Client) WorkNotificationV2GetSendResult(info WorkNotificationGetSendResult) (*WorkNotificationGetSendResultRsp, error) {
	rsp := new(WorkNotificationGetSendResultRsp)
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	// 获取 accessToken
	accessToken, err := c.GetAccessToken()
	if err != nil {
		return rsp, err
	}
	param := map[string]string{
		"access_token": accessToken,
	}
	url := fmt.Sprintf("%s?%s", MessageCorpconversationGetsendresult, dingtalkhttplib.URLEncode(param))
	log.Println(url)
	req := dingtalkhttplib.Post(url)
	// 添加请求body数据
	jsonData, err := json.Marshal(info)
	if err != nil {
		return rsp, err
	}
	str2 := string(jsonData[:])
	log.Println(str2)
	req.Body(jsonData)
	b, err := req.AsBytes()
	if err != nil {
		return rsp, err
	}
	str := (*string)(unsafe.Pointer(&b))
	log.Println(*str)
	err = json.Unmarshal(b, rsp)
	if err != nil {
		return rsp, err
	}
	if rsp.Errcode != 0 {
		return rsp, errors.New(rsp.Errmsg)
	}
	return rsp, nil
}
